package com.yj.ccs.abs.web.account.service;

import com.yj.ccs.abs.boot.pagination.Page;
import com.yj.ccs.abs.common.Constants;
import com.yj.ccs.abs.util.encryption.Digests;
import com.yj.ccs.abs.util.encryption.Encodes;
import com.yj.ccs.abs.web.management.dto.*;
import com.yj.ccs.abs.web.management.entity.User;
import com.yj.ccs.abs.web.management.mapper.ManagementPageMapper;
import com.yj.ccs.abs.web.management.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/14
 * Author     : Unbelievable9
 **/
@Service
public class UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    private final UserMapper userMapper;

    private final ManagementPageMapper pageMapper;

    @Autowired
    public UserService(UserMapper userMapper, ManagementPageMapper pageMapper) {
        this.userMapper = userMapper;
        this.pageMapper = pageMapper;
    }

    private User setupNewUser(CreateUserDto createUserDto, String creator) {
        User user = new User();

        if (userMapper.getByAccount(createUserDto.getAccount()) != null) {
            user.setAccount(createUserDto.getAccount());
            user.setExisted(true);

            return user;
        }

        user.setUsername(createUserDto.getUsername());
        user.setAccount(createUserDto.getAccount());
        user.setFirm(createUserDto.getFirm());
        user.setApartment(createUserDto.getApartment());
        user.setArea_code(createUserDto.getArea_code());
        user.setCellphone(createUserDto.getCellphone());
        user.setRole_type((createUserDto.getRole_type()));
        user.setActive(true);

        String initialPassword = "88888888";

        byte[] passwordSalt = Digests.generateSalt(Constants.PASSWORD_SALT_SIZE);
        byte[] hashPassword = Digests.sha1(initialPassword.getBytes(), passwordSalt, Constants.HASH_ITERATION_COUNT);

        user.setPassword(Encodes.encodeHex(hashPassword));
        user.setPassword_salt(Encodes.encodeHex(passwordSalt));
        user.setCreator(creator);

        return user;
    }

    public User getById(Long id) {
        return userMapper.getById(id);
    }

    public User getUserByAccount(String account) {
        return userMapper.getByAccount(account);
    }

    public Page<User> getUserPage(KeywordDto pageDto) {
        return pageMapper.getUserListOnCondition(pageDto);
    }

    public Boolean accountExist(String account) {
        return userMapper.getByAccount(account) != null;
    }

    public User createUser(CreateUserDto createUserDto, String creator) {
        User newUser = setupNewUser(createUserDto, creator);

        if (!newUser.getExisted()) {
            userMapper.create(newUser);

            return null;
        } else {
            return newUser;
        }
    }

    public String updateUser(UpdateUserDto updateUserDto, User user) {
        if (!user.getAccount().equals(updateUserDto.getAccount()) && accountExist(updateUserDto.getAccount())) {
            return updateUserDto.getAccount();
        } else {
            userMapper.update(updateUserDto);

            return null;
        }
    }

    public void changeUserPassword(String newPassword, Long userId) {
        byte[] passwordSalt = Digests.generateSalt(Constants.PASSWORD_SALT_SIZE);
        byte[] hashPassword = Digests.sha1(newPassword.getBytes(), passwordSalt, Constants.HASH_ITERATION_COUNT);

        String password = Encodes.encodeHex(hashPassword);
        String password_salt = Encodes.encodeHex(passwordSalt);

        userMapper.changePassword(userId, password, password_salt);

        logger.debug("重置后密码为: " + newPassword);
    }

    public void changeUserStatus(ChangeStatusDto changeStatusDto) {
        userMapper.changeStatus(changeStatusDto);
    }

    public void deleteUser(IdListDto idListDto) {
        userMapper.batchDelete(idListDto);
    }
}
